package com.sms.gui;

import java.awt.BorderLayout;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextPane;

import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;

import com.sms.gui.actions.ExitAction;
import com.sms.gui.actions.NewSmsAction;
import com.sms.gui.actions.SetDestinatarioAction;
import com.sms.gui.actions.SettingsAction;
import com.sms.gui.actions.SmsSendAction;
import com.sms.gui.listeners.MyWindowListener;
import com.sms.util.ResourcesHandler;

public class SMSFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JLabel destField;
    private JTextPane message;

    public SMSFrame() {
        super("Sms vodafone");
        init();
    }

    private void init() {
        // impoastazioni grafiche
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setIconImage(ResourcesHandler.ICON);
        setMinimumSize(ResourcesHandler.GUI_DIM);
        setPreferredSize(ResourcesHandler.GUI_DIM);
        setLocationByPlatform(true);
        setLayout(new BorderLayout());
        addWindowListener(new MyWindowListener());

        JPanel center = new JPanel(new BorderLayout());
        center.setBorder(BorderFactory
                .createTitledBorder("Testo del messaggio"));

        destField = new JLabel();

        center.add(new JScrollPane(message = new JTextPane()));

        Action nuovoSms = new NewSmsAction(message);
        Action setDestinatario = new SetDestinatarioAction(destField);
        Action sendSms = new SmsSendAction(destField, message);

        Action settings = new SettingsAction(this);
        Action exitAction = new ExitAction();

        JMenuBar menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        file.add(nuovoSms);
        file.add(new JSeparator());
        file.add(exitAction);

        menuBar.add(file);

        JMenu smsMenu = new JMenu("Sms");
        smsMenu.add(setDestinatario);
        smsMenu.add(sendSms);

        menuBar.add(smsMenu);

        JMenu confMenu = new JMenu("Configurazione");
        confMenu.add(settings);

        menuBar.add(confMenu);

        setJMenuBar(menuBar);

        JXTaskPaneContainer taskContainer = new JXTaskPaneContainer();
        taskContainer.setBorder(BorderFactory.createTitledBorder("Menu"));

        JXTaskPane smsTask = new JXTaskPane();
        smsTask.setTitle("Sms");
        smsTask.add(setDestinatario);
        smsTask.add(destField);
        smsTask.add(sendSms);

        taskContainer.add(smsTask);

        JXTaskPane optionsTask = new JXTaskPane();
        optionsTask.setTitle("Opzioni");
        optionsTask.add(settings);
        optionsTask.add(exitAction);

        taskContainer.add(optionsTask);

        getContentPane().add(taskContainer, BorderLayout.WEST);
        getContentPane().add(center, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }
}